<template>
  <div class="search">
    <h1>巡更路线管理</h1>
    <hr style="border: 1px solid rgb(240, 240, 240)" />
    <span>巡更路线名称</span>
    <el-input
      v-model="path_name"
      style="width: 200px; margin-right: 40%"
      placeholder="请输入"
    />
    <el-button type="primary" @click="get">查询</el-button>
    <el-button type="default" @click="reset">重置</el-button>
  </div>
  <div class="page">
    <div class="page-header">
      <div class="page-header-left">
        <span>路线列表</span>
      </div>
      <div class="page-header-right">
        <el-button
          type="primary"
          style="margin-top: 1rem"
          @click="to('patrol/newPath')"
          >+&nbsp;新建</el-button
        >
        <el-button
          type="default"
          style="margin-top: 1rem; margin-right: 3rem"
          @click="deleteDialog = true"
          >批量删除</el-button
        >
        <el-dialog v-model="deleteDialog" title="警告" width="500" align-center>
          <span>是否删除选中的信息，该操作不可撤回！</span>
          <template #footer>
            <div class="dialog-footer">
              <el-button @click="deleteDialog = false">取消</el-button>
              <el-button type="primary" @click="deleteByIds"> 确认 </el-button>
            </div>
          </template>
        </el-dialog>
      </div>
    </div>
    <el-table
      v-loading="loading"
      :data="tableData"
      style="width: 100%"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" />
      <el-table-column property="pathName" label="巡更路线名称" />
      <el-table-column property="pointName" label="巡更点" />
      <el-table-column property="pathStatus" label="状态">
        <template #default="{ row }">
          {{
            row.pathStatus == null || row.pathStatus == ""
              ? ""
              : "已" + row.pathStatus
          }}
        </template>
      </el-table-column>
      <el-table-column property="addPerson" label="添加人" />
      <el-table-column label="添加时间">
        <template #default="{ row }">
          {{ row.addTime[0] }}-{{
            row.addTime[1] < 10 ? "0" + row.addTime[1] : row.addTime[1]
          }}-{{ row.addTime[2] < 10 ? "0" + row.addTime[2] : row.addTime[2] }}
          {{ row.addTime[3] < 10 ? "0" + row.addTime[3] : row.addTime[3] }}:{{
            row.addTime[4] < 10 ? "0" + row.addTime[4] : row.addTime[4]
          }}:{{
            row.addTime[5] == null
              ? "00"
              : row.addTime[5] < 10
              ? "0" + row.addTime[5]
              : row.addTime[5]
          }}
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template #default="{ row }">
          <el-button type="text" @click="editor(row.id)">编辑</el-button>
          <el-button type="text" @click="deleteById(row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination">
      <el-pagination
        v-model:current-page="pageNum"
        v-model:page-size="pageSize"
        :page-sizes="[5, 10, 20]"
        style="margin-bottom: 2rem"
        background="true"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script setup>
import { ElMessage, ElTable } from "element-plus";
import to from "@/utils/to.js";
import { onMounted, ref } from "vue";
import axios from "axios";
import router from "@/router";

const loading = ref(false);
const deleteDialog = ref(false);

const path_name = ref("");

const pageNum = ref(1);
const pageSize = ref(10);
const total = ref(0);
const tableData = ref([]);
const selectRows = ref([]);

const reset = () => {
  path_name.value = "";
  get();
};

const handleSelectionChange = (selectedRows) => {
  selectRows.value = selectedRows;
};

const handleSizeChange = (newSize) => {
  pageSize.value = newSize;
  get();
};

const handleCurrentChange = (newNum) => {
  pageNum.value = newNum;
  get();
};

const get = () => {
  const config = {
    url: "http://localhost:8080/patrolPath/listPatrolPath",
    method: "POST",
    headers: {
      Authorization: localStorage.getItem("token"),
    },
    data: {
      pageNum: pageNum.value,
      pageSize: pageSize.value,
      pathName: path_name.value,
    },
  };

  loading.value = true;
  const response = axios(config);
  response
    .then((data) => {
      console.log(data);
      total.value = data.data.data.total;
      tableData.value = data.data.data.records;
      loading.value = false;
    })
    .catch((error) => {
      console.log(error);
    });
};

onMounted(() => {
  get();
});

const editor = (id) => {
  router.push({ path: "/backend/patrol/newPath", query: { id: id } });
};

const deleteById = (id) => {
  const config = {
    url: "http://localhost:8080/patrolPath/deletePatrolPath",
    method: "DELETE",
    headers: {
      Authorization: localStorage.getItem("token"),
    },
    params: {
      id: id,
    },
  };

  const response = axios(config);
  response
    .then((data) => {
      if (data.data.code === 1) {
        ElMessage({
          message: data.data.msg,
          type: "success",
        });
        setTimeout(() => {
          get();
        }, 2000);
      }
    })
    .catch((error) => {
      console.log(error);
    });
};

const deleteByIds = () => {
  if (selectRows.value.length === 0) {
    ElMessage({
      message: "请选择要删除的信息",
      type: "warning",
    });
    deleteDialog.value = false;
    return;
  }

  const config = {
    url: "http://localhost:8080/patrolPath/deleteByIds",
    method: "DELETE",
    headers: {
      Authorization: localStorage.getItem("token"),
    },
    data: selectRows.value.map((item) => item.id),
  };

  const response = axios(config);
  response
    .then((data) => {
      if (data.data.code === 1) {
        ElMessage({
          message: data.data.msg,
          type: "success",
        });
        setTimeout(() => {
          get();
        }, 2000);
      }
    })
    .catch((error) => {
      console.log(error);
    });
  deleteDialog.value = false;
};
</script>

<style scoped>
.search {
  background-color: white;
  padding-bottom: 2rem;
  height: auto;
  margin-top: 2rem;
}

h1 {
  font-size: 2rem;
  font-weight: 600;
  padding: 2rem 0 10px 4rem;
}

span {
  display: inline-block;
  font-size: 15px;
  margin-top: 2rem;
  margin-left: 3rem;
  margin-right: 1rem;
  height: 24px;
}

.page {
  background-color: white;
  height: auto;
  margin-top: 2rem;
  margin-bottom: 2rem;
}

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.pagination {
  display: flex;
  margin-top: 2rem;
  justify-content: center;
}
</style>
